//
//  ASDatePicker.m
//  AngelSound
//
//  Created by jumper on 15/8/21.
//  Copyright (c) 2015年 Jumper. All rights reserved.
//

#import "ASDatePicker.h"
#import "UIView+ASPhotoCategory.h"

@implementation ASDatePicker

//初始化
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.3;
    }
    return self;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [view hideAnimation];
    [self removeFromSuperview];
}

-(void)addDatePickerToCon:(id)controller andWindow:(id)win{
    
    //创建工具条
    UIToolbar *toolBar = [[UIToolbar alloc]init];
    //设置工具条的颜色
    toolBar.barTintColor= KHexColor(@"448AFF"); BaseColor;
    //设置工具条的frame
    toolBar.frame=CGRectMake(0, 0, kSCREEN_WIDTH, 40);
    
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, kSCREEN_WIDTH, 216)];
    // 设置地区 zh:中国
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"HH:mm"];
//    NSDate *date1 = [dateFormatter dateFromString:@"08:00"];
    
    //给工具条添加按钮
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 40, 40)];
    [button setTitleColor:RGBA(83, 83, 83, 1) forState:UIControlStateNormal];
    [button setTitle:HHH_Text(@"取消") forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    button.titleLabel.textAlignment = NSTextAlignmentLeft;
    [button addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *okbutton = [[UIButton alloc]initWithFrame:CGRectMake(kSCREEN_WIDTH-10-40, 0, 40, 40)];
    [okbutton setTitleColor:RGBA(83, 83, 83, 1) forState:UIControlStateNormal];
    [okbutton setTitle:HHH_Text(@"确定") forState:UIControlStateNormal];
    okbutton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    okbutton.titleLabel.textAlignment = NSTextAlignmentRight;
    [okbutton addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *noExpectButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 40, kSCREEN_WIDTH, 30)];
    [noExpectButton setTitleColor:RGBA(83, 83, 83, 1) forState:UIControlStateNormal];
    [noExpectButton setTitle:_titleStr forState:UIControlStateNormal];
    noExpectButton.titleLabel.font = [UIFont systemFontOfSize:14.0];

    
    [toolBar addSubview:button];
    [toolBar addSubview:okbutton];
    
//    if (_aType == ASDATE_TYPE_TITLE) {
//        
//        toolBar.height = 70;
//        self.datePicker.top = 70;
//        [toolBar addSubview:noExpectButton];
//        
//    }else{
//        
//        [noExpectButton removeFromSuperview];
//    }
    
    view = [[UIView alloc]initWithFrame:CGRectMake(0, kSCREEN_HEIGHT - (self.datePicker.frame.size.height + 40), kSCREEN_WIDTH, self.datePicker.frame.size.height + 40)];
    view.backgroundColor = [UIColor whiteColor];
    [view addSubview:self.datePicker];
    [view addSubview:toolBar];
    
    [win addSubview:view];
    
    [view showAnimation];
}
-(void)popCancel
{
    [view hideAnimation];
    [self removeFromSuperview];
   
}
- (void)cancle
{
    [view hideAnimation];
    [self removeFromSuperview];
    if (self.switchOffBlock) {
        self.switchOffBlock();
    }
    if (self.dietOffSwitchBlock) {
        self.dietOffSwitchBlock();
    }
}

-(void)commit{
    
    [view hideAnimation];
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(getDateFromDatePicker:)]) {
          [self.delegate getDateFromDatePicker:self.datePicker.date];
    }    
}

-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.backgroundColor = [UIColor clearColor];
    return label;
    
}
@end
